﻿using UnityEngine;
using System.Collections;

public class PersonComportament : MonoBehaviour
{

    private bool inDoor;
    private bool inTable;
    private bool getOut = false;
    private float verticalPosition;
    private int verticalDirection;

    private Vector3 StartPosition;
    private Vector3 DoorPosition;
    private Vector3 TablePosition;
    private Vector3 FinalPosition;
    
    public float speed = 5;
    public AudioClip sound;
    public float pitchVariance = 0.1f;
    private Animation _anim;
    private AudioSource _audio;
    // Use this for initialization
    void Start()
    {
        verticalDirection = 1;
        StartPosition = new Vector3(291f, 165f, 13f);
        DoorPosition = new Vector3(290.02f, 165f, 10.5f);
        TablePosition = new Vector3(290.02f, 165.02f, -1.5f);
        FinalPosition = new Vector3(289f, 165.02f, 13f);

        this.gameObject.transform.position = StartPosition;
        _anim = GetComponentInChildren<Animation>();
        _audio = gameObject.AddComponent<AudioSource>();
        _audio.playOnAwake = false;
        _audio.pitch = Random.RandomRange(1.0f - pitchVariance, 1.0f + pitchVariance);
    }

    // Update is called once per frame
    void Update()
    {
        if (!inTable)
            GoToMayor();
        else if(getOut)
            GoOutMayor();
    }

    private void GoToMayor()
    {
        if (inDoor == false)
            GoToDoor();
        else
            GoToTable();
    }

    private void GoOutMayor()
    {
        if (inDoor == false)
            GoToDoor();
        else
            GoToFinish();
    }

    private void GoToDoor()
    {
        if (GoToPositon(DoorPosition))
            inDoor = true;
    }

    private void GoToTable()
    {
        if (GoToPositon(TablePosition))
        {
            inTable = true;
            inDoor = false;
            ArrivedTable();
        }
    }

    private void GoToFinish()
    {
        if (GoToPositon(FinalPosition))
        {
            inTable = false;
            inDoor = false;
            this.gameObject.transform.position = StartPosition;
            ArrivedFinish();
        }
    }

    private bool GoToPositon(Vector3 target)
    {
        _anim.CrossFade("walk");
        var position = this.gameObject.transform.position;
        var newPosition = Vector3.MoveTowards(position, target, speed * Time.deltaTime);
        this.gameObject.transform.position = new Vector3(newPosition.x, StepEffect(), newPosition.z);
        return (Vector3.Distance(position, target) < 0.25F);
    }

    private float StepEffect()
    {
        verticalPosition += verticalDirection * 0.01f;
        if (Mathf.Abs(verticalPosition) >= 0.1f)
            verticalDirection *= -1;

        return StartPosition.y + verticalPosition;
    }

    public void StartGetOut()
    {
        getOut = true;
        _anim.transform.parent.localRotation = Quaternion.AngleAxis(180, Vector3.up);
    }

    public void ArrivedTable()
    {
        _anim.CrossFade("idle");
        _audio.PlayOneShot(sound);
        GetComponent<ProblemHolder>().SetDraw();
        GameObject.Find("Manager").GetComponent<ProblemManager>().CheckCorrupt(this);

    }
    public void ArrivedFinish()
    {
        getOut = false;
        _anim.CrossFade("idle");
        GameObject.Find("Manager").GetComponent<ProblemManager>().Next();
    }
}